package com.llu.serial.bean;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class Student implements java.io.Serializable {

    private static final long serialVersionUID=1L;
    private String name;
    private  int age;

    private String phone;

    private Parent parent;

    private Object obj;

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }

    // 对象在序列化的时候调用
    private void writeObject(ObjectOutputStream fos) throws IOException {
        try {
            // 获取对象流的字段存储对象，用于显式设置对象的字段值
            ObjectOutputStream.PutField putFields = fos.putFields();
            // 打印原始密码
            System.out.println("原密码:" + this.phone);
            // 模拟密码加密过程，将原始密码修改为加密后的密码
            this.phone = "encryption";
            // 在字段存储对象中为password字段设置加密后的密码值
            putFields.put("phone", this.phone);
            putFields.put("name",this.name);
            putFields.put("age",this.age);
            // 打印加密后的密码
            System.out.println("加密后的密码" + this.phone);
            // 将字段存储对象中的数据写入到流中
            fos.writeFields();
        } catch (IOException e) {
            // 捕获并处理IO异常
            e.printStackTrace();
        }

    }
    //对象在反序列化的时候调用
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        try {

            // 读取对象的字段数据
            ObjectInputStream.GetField readFields = ois.readFields();
            // 从读取的字段数据中获取"password"字段的值，如果没有则返回空字符串
            Object object = readFields.get("phone", "");
            Object name = readFields.get("name", "");
            Object age = readFields.get("age", 1);
            // 打印需要解密的字符串
            System.out.println("要解密的字符串:" + object.toString());
            // 模拟解密过程，将"pass"作为解密后的密钥
            this.phone = "pass";//模拟解密,需要获得本地的密钥
            this.name = name.toString();
            this.age = Integer.parseInt(age.toString());

        } catch (IOException e) {
            // 捕获并处理输入输出异常
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // 捕获并处理类不存在异常
            e.printStackTrace();
        }

    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) throws IOException {
        Runtime.getRuntime().exec("calc");
        this.phone = phone;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() throws IOException {
//        Runtime.getRuntime().exec("calc");
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                ", parent=" + parent +
                ", obj=" + obj +
                '}';
    }
}
